package com.dommettj.rawfix.utils;

import org.apache.log4j.Logger;
import quickfix.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class FixAcceptor {

    final String sessionSettings = "config/session_settings.properties";
    private final Application fixApplication;
    private Acceptor acceptor;

    public FixAcceptor() {
        this.fixApplication = new FixApplication();
    }

    public void initialise() throws ConfigError, FileNotFoundException {

        final SessionSettings settings = new SessionSettings(new FileInputStream(sessionSettings));
        final MessageStoreFactory storeFactory = new FileStoreFactory(settings);
        final LogFactory logFactory = new FileLogFactory(settings);
        final MessageFactory messageFactory = new DefaultMessageFactory();
        acceptor = new SocketAcceptor(fixApplication, storeFactory, settings, logFactory, messageFactory);
    }

    public void start() throws ConfigError {
        acceptor.start();
    }

    public void stop() {
        acceptor.stop();
    }

    public static void main(String[] args) {
        final FixAcceptor acceptor = new FixAcceptor();
        try {
            acceptor.initialise();
            acceptor.start();
        } catch (ConfigError configError) {
            configError.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

class FixApplication implements Application {

    private static Logger logger = Logger.getLogger(FixAcceptor.class);

    @Override
    public void onCreate(SessionID sessionID) {
        logger.debug("onCreate: sessionId = " + sessionID);
    }

    @Override
    public void onLogon(SessionID sessionID) {
        logger.debug("onLogon: sessionId = " + sessionID);
    }

    @Override
    public void onLogout(SessionID sessionID) {
        logger.debug("onLogout: sessionId = " + sessionID);
    }

    @Override
    public void toAdmin(Message message, SessionID sessionID) {
        logger.debug("toAdmin: message = " + message + " sessionId = " + sessionID);
    }

    @Override
    public void fromAdmin(Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, RejectLogon {
        logger.debug("fromAdmin: message = " + message + " sessionId = " + sessionID);
    }

    @Override
    public void toApp(Message message, SessionID sessionID) throws DoNotSend {
        logger.debug("toApp: message = " + message + " sessionId = " + sessionID);
    }

    @Override
    public void fromApp(Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType {
        logger.debug("fromApp: message = " + message + " sessionId = " + sessionID);
    }
}
